package com.gec.oasys.controller;

import com.gec.oasys.pojo.SysMenu;
import com.gec.oasys.pojo.SysUser;
import com.gec.oasys.service.SysMenuService;
import com.gec.oasys.util.SoulPage;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;
    @RequestMapping("/findSysMenuById")
    @ResponseBody
    public Map<String, Object> findSysMenuById(Integer id){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        SysMenu sysMenu=sysMenuService.selectByPrimaryKey(id);
        code="0";msg="成功";
        mapData.put("code",code);
        mapData.put("msg",msg);
        mapData.put("data",sysMenu);
        return mapData;
    }
    @RequestMapping("/toSysMenuList")
    public String toSysMenuList(){
        return "sysmenu/sysmenulist";
    }
    @RequestMapping("/deleteSysMenuById")
    @ResponseBody
    public Map<String,Object> deleteSysMenuById(Integer id){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        int pos =sysMenuService.deleteByPrimaryKey(id);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/deleteSysMenusById")
    @ResponseBody
    public Map<String,Object> deleteSysMenusById(String ids){
        HashMap<String, Object> mapData = new HashMap<>();
        String code="1",msg="内部错误";
        String strids[]=ids.split(",");
        int pos =sysMenuService.deletesByPrimaryKey(strids);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/toSysMenuAdd")
    public String toSysMenuAdd(){
        return "sysmenu/sysmenuadd";
    }
    @RequestMapping("/saveSysMenu")
    @ResponseBody
    public Map<String,Object> saveSysMenu(SysMenu sysMenu){
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        int pos=sysMenuService.insert(sysMenu);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/updateBySysMenuPrimaryKey")
    @ResponseBody
    public Map<String,Object> updateByPrimaryKey(SysMenu sysMenu){
        Map<String,Object> mapData=new HashMap<>();
        String code="1",msg="内部错误";
        int pos=sysMenuService.updateByPrimaryKey(sysMenu);
        if(pos>0){
            code="0";msg="成功";
        }
        mapData.put("msg",msg);
        mapData.put("code",code);
        return mapData;
    }
    @RequestMapping("/getDataBySysMenu")
    @ResponseBody
    public Object dataGrid(SoulPage<SysMenu> soulPage, SysMenu sysMenu) {
        soulPage.setObj(sysMenu);
        return sysMenuService.dataGrid(soulPage);
    }
    @RequestMapping("/getMenuDataTreeByRoleIdJson")
    @ResponseBody
    public List<SysMenu> getMenuDateTreeByRoleIdJson(Integer roleId){
        return sysMenuService.getSysMenuTreeDateById(75,roleId);
    }
    @RequestMapping("/getMenuDataTreeJson")
    @ResponseBody
    public List<SysMenu> getMenuDataTreeJson() {
        return sysMenuService.getSysMenuTreeDataById(75);
    }

    @RequestMapping("/getMenuTop")
    @ResponseBody
    public List<SysMenu> getMenuTop()
    {
        //获取当前用户身份认证成功后的对象信息
        SysUser user= (SysUser) SecurityUtils.getSubject().getPrincipal();

        //获取顶级菜单数据
        //条件：is_top=1 并且 del_flag=0
        SysMenu sysMenu=new SysMenu();
        sysMenu.setIs_top("1");
        sysMenu.setRoleId(user.getRoleId());

        return sysMenuService.query(sysMenu);
    }


    @RequestMapping("/getMenuByPidRec")
    @ResponseBody
    public List<SysMenu> getMenuByPidRec(SysMenu sysMenu)
    {
        //获取当前用户身份认证成功后的对象信息
        SysUser user= (SysUser) SecurityUtils.getSubject().getPrincipal();
        sysMenu.setRoleId(user.getRoleId());
        return sysMenuService.queryNavMenuTree(sysMenu);
    }

    @RequestMapping("/getTopMenuFirst")
    @ResponseBody
    public String getTopMenuFirst()
    {
        //获取当前用户身份认证成功后的对象信息
        SysUser user= (SysUser) SecurityUtils.getSubject().getPrincipal();

        //获取顶级菜单数据
        //条件：is_top=1 并且 del_flag=0
        SysMenu sysMenu=new SysMenu();
        sysMenu.setIs_top("1");
        sysMenu.setRoleId(user.getRoleId());

        List<SysMenu> sysMenuList=sysMenuService.query(sysMenu);

        if(sysMenuList!=null && sysMenuList.size()>0)
        {
            return String.valueOf(sysMenuList.get(0).getId());
        }

        return null;

    }
}
